﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TFSTools.TFSLibrary.TestManagement;

namespace TFSTools.TestManagement.TestCaseReview
{
    public class TestPlanSorter : IComparable
    {
        private readonly TFSTestPlan testPlan;
        private readonly string testPlanComparer;

        public TestPlanSorter(TFSTestPlan testPlan)
        {
            this.testPlan = testPlan;
            this.testPlanComparer = testPlan.Name;
        }

        public TFSTestPlan TestPlan
        {
            get { return this.testPlan; }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            TestPlanSorter other = obj as TestPlanSorter;
            if (other == null)
            {
                throw new Exception("The method or operation is not implemented.");
            }
            else
            {
                return string.Compare(this.testPlanComparer, other.testPlanComparer);
            }
        }

        #endregion

        public static List<TFSTestPlan> Sort(List<TFSTestPlan> list)
        {
            var sorterList = new List<TestPlanSorter>(list.Count);
            list.ForEach(testPlan => sorterList.Add(new TestPlanSorter(testPlan)));
            sorterList.Sort();

            var sortedList = new List<TFSTestPlan>(list.Count);
            sorterList.ForEach(testPlan => sortedList.Add(testPlan.TestPlan));
            return sortedList;
        }
    }
}
